package com.fc.remoting.message;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;

import com.fc.remoting.exception.HeaderCheckFailedException;

public class RemotingHeader {
	private static AtomicInteger requestId = new AtomicInteger(0);
	private int id = requestId.getAndIncrement();
	private byte checkCode;
	private byte version = 0x01;
	private byte msgType = 0;
	// 加密方式，默认不加密
	private byte encryptType = 0x00;
	// 压缩方式，默认不压缩
	private byte compressType = 0x00;

	public static RemotingHeader createHeader(ByteBuffer byteBuffer) throws HeaderCheckFailedException {
		RemotingHeader header = new RemotingHeader();
		header.checkCode = byteBuffer.get();
		header.version = byteBuffer.get();
		header.msgType = byteBuffer.get();
		header.encryptType = byteBuffer.get();
		header.compressType = byteBuffer.get();

		return header;
	}

	public int getId() {
		return id;
	}
	
	//通过header部分生成的checkcode
	public byte getCheckCodePart() {
		return (byte) (version ^ msgType ^ encryptType ^ compressType);
	}
	
	public byte getCheckCode() {
		return checkCode;
	}
	
	public byte getVersion() {
		return version;
	}

	public void setVersion(byte version) {
		this.version = version;
	}

	public byte getMsgType() {
		return msgType;
	}

	public void setMsgType(byte msgType) {
		this.msgType = msgType;
	}

	public byte getEncryptType() {
		return encryptType;
	}

	public void setEncryptType(byte encryptType) {
		this.encryptType = encryptType;
	}

	public byte getCompressType() {
		return compressType;
	}

	public void setCompressType(byte compressType) {
		this.compressType = compressType;
	}
}
